// 商品管理脚本
$(function() {
	$("#searchid").linkbutton({
		 text:'查询',
		 iconCls:'icon-search'
	});
	$('#messagegrid').datagrid({
		title : '您所在的位置：资讯知识管理模块&nbsp;&nbsp;--&gt;&gt;&nbsp;&nbsp;资讯广告管理',
		idField : 'id',
		width : 1000,
		height : 400,
		nowrap : false,
		striped : true,
		collapsible : false,
		url : '../brand/findAllBrand.shtml',
		frozenColumns : [[{
					field : 'ck',
					title : '选择',
					checkbox : true
				}]],
		columns : [[{
					field : 'name',
					title : '品牌名称',
					align : 'center',
					width : 162
				},{
					field : 'imgsrc',
					title : '品牌logo',
					align : 'center',
					width : 162,
					formatter:function(value,rec){
						return '<img width="160px" height="40px" src="../'+value+'">';
					}
				}, {
					field : 'code',
					title : '权重',
					align : 'center',
					width : 145,
					sortable : true
				}, {
					field : 'id',
					title : '操作',
					align : 'center',
					width : 160,
					sortable : true,
					formatter : function(value, rec) {
						var id = rec.id;
						var weights = rec['code'];
						if (rec.id == undefined)
							return null;
						else
							return '<span style="color:red" id="operate"><a href="javascript:modify(\''+weights+'\',\''+id+'\')">修改</a> <a href="javascript:del(\''+id+'\')">删除</a></span>';
					}
				}
		]],
		pagination : true,
		rownumbers : true,
		toolbar : [{
					id : 'btnadd',
					text : '添加信息',
					iconCls : 'icon-add',
					handler : function() {
						$(this).attr("href", '../brand/showBrandCategoryWeights.shtml?menuCls=31');
					}
				},'-',{
					id : 'btnclose',
					text : '  &nbsp;关  闭',
					iconCls : 'icon-cancel',
					handler : function() {
						window.self.close();
					}
				}]
	}

	);

	
	$.get("../brand/showFirstCategory.shtml",function(mes){
		if(mes!=''){
			var json = eval('('+mes+')');
			var str = '<select name="bigCategory" onchange="bigCategoryChange()" id="bigCategory" >';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
			str+='</select>';
			$('#categoryBigSpan').html(str);
			bigCategoryChange();
		}
		
	})
	$("#modifyWindow").dialog({
		title:'修改',
		width:300,
		height:120,
		resizable:false,
		minimizable:false,
		maximizable:false,
		toolbar: [{
			text : '保存信息',
		    iconCls:'icon-save',
		    handler:function(){
		    		var categoryId = $('#categoryId option:selected').val();
		    		var typeId = $('#typeId option:selected').val();
						var url1 = "../brand/updateBrandCategoryWeights.shtml";
						var weights = $("#weightValue").val();
						if(isNaN(weights)){
							alert("权重值只能为数字");
							return;
						}
						var brandId = $("#brandIdValue").val();
						$.ajax({
							url : url1,
							data : "brandId=" + brandId+"&categoryId="+categoryId+"&weights="+weights+"&typeId="+typeId,
							success : function(msg) {
								if(msg=="1"){
									alert("保存成功");							
								}else{
									alert("保存失败");
								}
									$("#modifyWindow").dialog('close');
								$('#messagegrid').datagrid("reload");
								
							}
						});
		    }
		  }],
		closed:true
	})
});

 function bigCategoryChange(){
	var bigCategoryValue = $("#bigCategory option:selected").val();
	if(bigCategoryValue != ""){
		$('#smallCategory').html('加载中... ...');
		$.get('../category/category_loadCategoryByPid.shtml?id='+bigCategoryValue+'&t='+new Date().getTime().toString(36),function(mes){
			var json = eval('('+mes+')');
			var str = '<select name="categoryId" id="categoryId" onchange="smallCategoryChange();" >';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
			str+='</select>';
			$('#smallCategory').html(str);
			onSearch();
			smallCategoryChange();
		})
	}
}

function smallCategoryChange(){
	var smallCategoryValue = $("#categoryId option:selected").val();
	if(smallCategoryValue != ""){
		$('#goodsType').html('加载中... ...');
		$.get('../type/type_loadTypeByCategoryId.shtml?categoryId='+smallCategoryValue+'&t='+new Date().getTime().toString(36),function(mes){
			var json = eval('('+mes+')');
			var str = '<select name="typeId" id="typeId" onchange="threechange();" ><option value="">请选择</option>';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
			str+='</select>';
			$('#goodsType').html(str);
		})
	}
}


function del(id) {
	if (id == "undefined") {
		alert("此条记录已删除");
		return;
	}
	if (confirm("确定删除此条信息吗?")) {
		var categoryId = $('#categoryId option:selected').val();
		var typeId = $('#typeId option:selected').val();
		var url1 = "../brand/deleteInformationAd.shtml";
		$.ajax({
					url : url1,
					data : "brandId=" + id+"&categoryId="+categoryId+"&typeId="+typeId,
					success : function(msg) {
						if(msg=="1"){
							alert("删除成功");							
						}else{
							alert("删除失败");
						}
						$('#messagegrid').datagrid("reload");
					}
				});

	}
}
function modify(weights,brandId) {
	$("#weightValue").val(weights);
	$("#brandIdValue").val(brandId);
	$("#modifyWindow").window('open');
}

function onSearch(){
	var categoryId = $('#categoryId option:selected').val();
	var typeId = $('#typeId option:selected').val();
	if(categoryId==""){
		alert("请选择类别");
		return;
	}
	var queryParams =$('#messagegrid').datagrid("options").queryParams;
	queryParams.categoryId=categoryId;
	queryParams.typeId=typeId;
	$('#messagegrid').datagrid("reload");
}